﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using DataEditor.BuisnessLayer;
using DataEditor.Components;
using System.Reflection;
using DataLayer.Model.Classes;
using DataLayer;

namespace DataEditor
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private Occupations _data;
        private DataManager _manager;
        private bool[] _expanderStates;
        public MainWindow()
        {
            InitializeComponent(); 
            LoadData();
            InitControls();
        }
        private void LoadData()
        {
            _manager = new DataManager();
            _data = _manager.ReadData();
            _data.Occupation.Sort(new Comparator()); 
            _expanderStates =new bool[_data.Occupation.Count];
        }
        private void InitControls()
        {
          
            int i = 0;
            foreach (var occ in _data.Occupation)
            {
                var elem = UIBuilder.FormExpanderFromOccupation(occ,_expanderStates[i++]);
                foreach (SkillInList skillcomtrol in ((StackPanel)elem.Content).Children)
                {
                    skillcomtrol.SkillDeleted += new EventHandler(ReloadData);
                    skillcomtrol.SkillSelected += new EventHandler(SelectSkill);
                }

                spOccupationOverView.Children.Add(elem);
            }
            //TODO:Add option to new occupation

        }
        private void SaveData(){
            if(_data != null && _data.Occupation.Count >0)
            _manager.SaveData(_data);
        }

        private void SelectSkill(object sender,EventArgs e)
        {
            if (dpSkillInfo.Children.Count > 0 && dpSkillInfo.Children[0] != null)
               dpSkillInfo.Children.Remove(dpSkillInfo.Children[0]);
            var skilldescription = new SkillDescriptionComponent(sender as OccupationsOccupationSkill);
            skilldescription.SkillDataChanged += new EventHandler(UpdateData);
            dpSkillInfo.Children.Add(skilldescription);
        }

        private void ReloadData(object sender,EventArgs e){
            spOccupationOverView.Children.RemoveRange(0,spOccupationOverView.Children.Count);
            FilterTemplateSkills();
            InitControls();
        }
        private void UpdateData(object sender, EventArgs e)
        {
            int i = 0;
            foreach (Expander exp in spOccupationOverView.Children)
                _expanderStates[i++] = exp.IsExpanded;
            spOccupationOverView.Children.RemoveRange(0, spOccupationOverView.Children.Count);
            InitControls();
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            FilterTemplateSkills();
            SaveData();
        }

        private void FilterTemplateSkills()
        {
            foreach (var occ in _data.Occupation) {
                foreach (var skill in occ.Skills.ToList()){
                    if ((skill.AccessLvl == "0" ||
                       skill.Description == "Double click to change description" ||
                       skill.Name == "Select name"))
                        occ.Skills.Remove(skill);
                }
            }

        }
        private bool AlreadyExists(List<OccupationsOccupation> pList, string pItemName) {
            foreach (var occ in pList)
                if (occ.Name.Equals(pItemName))
                    return true;
            return false;
        }

        private void AddFromReflection_Click(object sender, RoutedEventArgs e)
        {
            var types = Assembly.GetAssembly(typeof(Character)).GetTypes().Where(t => t.IsSubclassOf(typeof(Character))).ToList();
            foreach (var type in types){
                if (!AlreadyExists(_data.Occupation, type.Name))
                    _data.Occupation.Add(new OccupationsOccupation
                    {
                        Name = type.Name,
                        Skills = new List<OccupationsOccupationSkill>()
                    });
            }
            ReloadData(null, null);
        }
    }
}
